package com.shuqi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.shuqi.activity.viewport.CommonTitle;
import com.shuqi.common.Config;
import com.shuqi.common.ControlHandler;
import com.shuqi.common.FunctionsSwitch;
import com.shuqi.common.RedDotProvider;
import com.shuqi.common.utils.SharedPreferenceUtil;
import com.shuqi.common.utils.StringUtils;
import com.shuqi.controller.MainController;
import com.shuqi.controller.R;
import com.shuqi.controller.UserSettingController;
import com.shuqi.database.dao.impl.VersionShowDao.VersionShowEnum;
import com.shuqi.interfaces.EventTypeConstants;
import com.shuqi.interfaces.IEventListener;
import com.shuqi.model.bean.AppUpdateInfo;
import com.shuqi.model.net.listener.HttpRequestErrListener;
import com.shuqi.statistics.StatisticsEventID;
import com.shuqi.statistics.StatisticsUtils;

/**
 * 设置界面
 * 
 * @author linyuren
 * 
 */
public class UserSettingActivity extends ActivityBase implements HttpRequestErrListener,
        IEventListener, OnClickListener {

    private final static int init = EventTypeConstants.BASE;
    private final static int initDataHotSuccess = init + 1;
    private CommonTitle mTitle;
    /** 设置控制器 */
    private UserSettingController controller;

    private AppUpdateInfo info = null;

    ControlHandler handler = new ControlHandler(this) {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case init:
                    activityLogicForward();
                    break;
                case initDataHotSuccess:
                    activityInitData();
                    break;
                default:
                    break;
            }
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.controller =
                (UserSettingController) MainController.getMainManagerInstance().createController(
                        MainController.CONTROLLER_ID_USERSETTINGACTIVITY, this);
        this.controller.addEventListener(this);
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.act_user_setting);
        handler.sendEmptyMessage(init);
        checkOfferWall();
    }
    
    /**检查推广墙开关*/
    private void checkOfferWall(){
        if(FunctionsSwitch.isOfferWallOpen(this)){
            findViewById(R.id.ll_Offer_Wall).setVisibility(View.VISIBLE);
        }else{
            findViewById(R.id.ll_Offer_Wall).setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left:
                ActivityManager.getInstance().finishActivity(this);
                break;
//            case R.id.my_message_info: {
//                Intent intent = new Intent(this, MyAccountDataActivity.class);
//                ActivityManager.getInstance().startNextActivity(intent, this);
//                StatisticsUtils.onEvent(this, StatisticsEventID.ACCOUNT_USER_INFORMATION);
//                break;
//            }
            case R.id.app_setting: {
                Intent intent = new Intent(this, SoftwareSettingActivity.class);
                ActivityManager.getInstance().startNextActivity(intent, this);
                StatisticsUtils.onEvent(this, StatisticsEventID.ACCOUNT_SOFTWARE_SETTING);
                break;
            }
            case R.id.about_us: {
                Intent intent = new Intent(this, AboutShuqiActivity.class);
                intent.putExtra("AppUpdateInfo", info);
                ActivityManager.getInstance().startNextActivity(intent, this);
                StatisticsUtils.onEvent(this, StatisticsEventID.ACCOUNT_ABOUT_SHUQI);
                break;
            }
            case R.id.user_feed_back:{
                Intent intent = new Intent(this, FeedBackActivity.class);
                ActivityManager.getInstance().startNextActivity(intent, UserSettingActivity.this);
            }
            break;
            case R.id.Offer_Wall: {
//                Intent intent = new Intent(this, OfferWallActivity.class);
                Intent intent = new Intent(this, OfferWallTopActivity.class);
                ActivityManager.getInstance().startNextActivity(intent, this);
                StatisticsUtils.onEvent(this, StatisticsEventID.ACCOUNT_BOUTIQUE_SOFTWARE);
                break;
            }
            case R.id.switch_number: {
                LoginActivity.openLogin(this);
                StatisticsUtils.onEvent(this, StatisticsEventID.ACCOUNT_CHANGE_USERNUM);
                break;
            }
        }
    }

    @Override
    public void onEvent(int event, Object data) {
        this.info = (AppUpdateInfo) data;
        this.handler.sendEmptyMessage(initDataHotSuccess);
    }

    @Override
    public void onResume() {
        super.onResume();
        boolean isShow = SharedPreferenceUtil.isSoftUpdate(this);
        boolean newPullVersion =
                RedDotProvider.getInstance().isShowRedDot(VersionShowEnum.new_pull_version_red_dot);
        
        ((TextView)findViewById(R.id.about_us_tv)).setCompoundDrawablesWithIntrinsicBounds
            (0, 0, isShow&&newPullVersion?R.drawable.red_point:0, 0);
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void activityLogicForward() {
        mTitle = (CommonTitle) findViewById(R.id.title);
        mTitle.setLeftClickListener(this);
//        this.findViewById(R.id.my_message_info).setOnClickListener(this);
        this.findViewById(R.id.app_setting).setOnClickListener(this);
        this.findViewById(R.id.about_us).setOnClickListener(this);
        this.findViewById(R.id.Offer_Wall).setOnClickListener(this);
        this.findViewById(R.id.switch_number).setOnClickListener(this);
        this.findViewById(R.id.user_feed_back).setOnClickListener(this);
        
        this.controller.handle(0, "");
    }

    @Override
    public void activityInitData() {
        int visible = View.GONE;
        if (this.info != null && StringUtils.isNotBlank(info.getIntro2())) {
            int oldVersion = Integer.valueOf(Config.VERSION_INFO);
            int newVersion = Integer.valueOf(info.getVersion2());
            if (oldVersion < newVersion) {
                visible = View.VISIBLE;
            }
        }
//        this.findViewById(R.id.has_new_version).setVisibility(visible);
    }

    @Override
    public void request_content_io() {
        // TODO Auto-generated method stub

    }

    @Override
    public void request_reponse_io() {
        // TODO Auto-generated method stub

    }

    @Override
    public void request_parse_io() {
        // TODO Auto-generated method stub

    }

    @Override
    public void request_parse_format() {
        // TODO Auto-generated method stub

    }

    @Override
    public void request_net_error() {
        // TODO Auto-generated method stub

    }

    @Override
    public void request_timeout() {
        // TODO Auto-generated method stub

    }

    @Override
    public void request_url() {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            ActivityManager.getInstance().finishActivity(this);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
